package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private INursingPlanService iNursingPlanService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        //所有护理计划id
        Set<Long> planIds = nursingLevels.stream().map(NursingLevel::getLplanId).collect(Collectors.toSet());
        List<NursingPlan> nursingPlans = iNursingPlanService.lambdaQuery()
                .in(NursingPlan::getId, planIds)
                .list();
        Map<Long, String> planMap = nursingPlans.stream().collect(Collectors.toMap(NursingPlan::getId, NursingPlan::getPlanName));
        nursingLevels.forEach(level -> {
            level.setPlanName(planMap.get(level.getLplanId()));
        });
        return nursingLevels;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        boolean result = save(nursingLevel);
        if (result){
            redisTemplate.delete(CacheConstants.CACHE_KEY_NURSING_LEVEL);
        }
        return result ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        boolean result = updateById(nursingLevel);
        if (result){
            redisTemplate.delete(CacheConstants.CACHE_KEY_NURSING_LEVEL);
        }
        return  result ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        boolean result = removeByIds(Arrays.asList(ids));
        if (result){
            redisTemplate.delete(CacheConstants.CACHE_KEY_NURSING_LEVEL);
        }
        return  result ? 1 : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        boolean result = removeById(id);
        if (result){
            redisTemplate.delete(CacheConstants.CACHE_KEY_NURSING_LEVEL);
        }
        return  result ? 1 : 0;
    }

    /**
     * 查询所有启用的护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevel> listAll() {
        // 1.先从缓存中获取
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get(CacheConstants.CACHE_KEY_NURSING_LEVEL);
        // 2.缓存中存在则返回
        if (list != null) {
            return list;
        }
        // 3.缓存中不存在则从数据库中查询,并将结果保存到缓存中
        List<NursingLevel> listDB = lambdaQuery().eq(NursingLevel::getStatus, 1)
                .orderByDesc(NursingLevel::getCreateTime)
                .list();
        redisTemplate.opsForValue().set(CacheConstants.CACHE_KEY_NURSING_LEVEL, listDB);
        return listDB;
    }
}
